package de.homelabs.bookstore.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.h2.constant.SysProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.util.UrlPathHelper;

import de.homelabs.bookstore.domain.Category;
import de.homelabs.bookstore.domain.News;
import de.homelabs.bookstore.persistence.TemplateElementDAO;
import de.homelabs.bookstore.web.ui.HLMenuManager;
import de.homelabs.bookstore.web.ui.MenuEntry;
import freemarker.template.Template;

@Controller
public class BookStoreIndexController {
	@Autowired
	TemplateElementDAO teDAO;
	
	@Autowired
	@Qualifier("freemarkerConfig")
	private FreeMarkerConfigurer freemarkerConfig;
	
	@Autowired
	@Qualifier("hlMenuManager")
	private HLMenuManager hlMenuManager;
	
	@RequestMapping("/news.htm")
	public ModelAndView indexHandler(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//buffer for parsed content
		StringBuffer content = new StringBuffer();
		
		//main model map
		Map<String, Object> modelData = new HashMap<String, Object>();
		
		/**
		 * erstes Modul, News
		 */
		/*
		 * Start: add News 
		 */
		Map<String, Object> newsModel = new HashMap<String, Object>();
		List<News> newsList = new ArrayList<News>();
		newsList.add(new News("May 25, 2011", "Dirk M&uuml;ller", "<p>Herzlich Willkommen im BookStore. Dies ist eine J2EE Beispielanwendung!</p>"+
				"Sed lacus. Donec lectus. Nullam pretium nibh ut turpis. Nam bibendum. In nulla tortor, elementum vel, tempor at, varius non, purus. Mauris vitae nisl nec metus placerat consectetuer. Donec ipsum. Proin imperdiet est. <p class=\"links\"><a href=\"#\">Read More</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\">Comments</a></p>"));
		newsModel.put("newslist", newsList);
		
		//integration des Modul templates
		Template myTemplate = freemarkerConfig.getConfiguration().getTemplate("news.ftl");
		//paresen des Modul Templates mit Modul Model-Map
		String newsBuffer = FreeMarkerTemplateUtils.processTemplateIntoString(myTemplate, newsModel);
		//zum Maincontent hinzufuegen
		content.append(newsBuffer);
		/*
		 * End: add News 
		 */
		
		
		/*
		 * Start: categories data
		 */
		List<Category> categoriesList = new ArrayList<Category>();
		categoriesList.add(new Category("erste Kategorie"));
		//into main model map
		modelData.put("categoryList", categoriesList);
		/*
		 * End: categories data
		 */
		
		/*
		 * Start: mainMenu
		 */
		List<MenuEntry> mainMenu = hlMenuManager.getMainMenuEntries();
		//into main model map
		modelData.put("menuEntries", mainMenu);
		/*
		 * End: mainMenu
		 */
		
		//main content to main model 
		modelData.put("content", content.toString());
		
		//parse view with main model map and template key
		return new ModelAndView("test.ftl", modelData);
	}
	
	@RequestMapping("/**/*.htm")
	public ModelAndView index2Handler(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//buffer for parsed content
		StringBuffer content = new StringBuffer();
				
		Map<String, Object> defaultModel = new HashMap<String, Object>();
		defaultModel.put("categoryList", new ArrayList<Category>());
		
		/*
		 * Start: mainMenu
		 */
		List<MenuEntry> mainMenu = hlMenuManager.getMainMenuEntries();
		//into main model map
		defaultModel.put("menuEntries", mainMenu);
		/*
		 * End: mainMenu
		 */
		
		
		//extract url path
		UrlPathHelper urlPathHelper = new UrlPathHelper();
		String lookuppath = urlPathHelper.getLookupPathForRequest(request);
		
		if (lookuppath.equalsIgnoreCase("/impressum.htm")){
			content.append("<p><h1>Impressum</h1></p>");
			content.append("<p>Hier steht das Impresuum</p>");
		} else {
		/*
		 * testcontent
		 */
		content.append("<p><h1>Willkommen in der Beispielanwendung!</h1></p>");
		content.append("<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor" 
				+ "invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo"
				+ "dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." 
				+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore"
				+ "et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum."
				+ "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>");
		}
		
		defaultModel.put("content",content.toString());
		
		//FreeMarkerCascadedView
		return new ModelAndView("test.ftl", defaultModel);
		
		//HLFreeMarkerViewWithTemplateManager
		//return new ModelAndView("default", modelData);
	}
}
